package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class AdressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{

    private ArrayList<Person> addressCollection; // dette er ArrayListen for Person.
    private static AddressBook instance;
    /**
     * Constructor for objects of class AdressBook
     */
    public AddressBook()
    {

        addressCollection = new ArrayList<Person>(); 
    }

    public static AddressBook getInstance()
    {
        if(instance == null){
            instance = new AddressBook();
        }
        return instance; 
    }

    public void addPerson(Person newPerson)
    {
        addressCollection.add(newPerson);
    }

    /**
     *  Searches the container for a person with specified id
     *  @param id id to search for
     *  @return the person with the ID, or null if not found.
     *  KNP
     **/

    public Person getPerson(long id)
    {
        boolean found = false;
        int index = 0;
        Person currentPerson = null;
        while(!found && index < addressCollection.size())
        {
            currentPerson = addressCollection.get(index); // gets the container
            if(currentPerson.getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return currentPerson;
        }
        else
        {
            return null;
        }

    }
    
    public void deletePerson(long id)
    {
        Person p = getPerson(id);
        addressCollection.remove(p);
    }
    
    public void listAllPersons()
    {
        for(Person x: addressCollection){
            System.out.println("Name: "+ x.getName() +"\t" +"ID: " +x.getId() + "\t" + "Phone: "+ x.getPhone());
        }
    }
}
